package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mapper.PermissionMapper;
import com.itany.mapper.RoleMapper;
import com.itany.pojo.Permission;
import com.itany.pojo.Role;
import com.itany.service.RoleService;
import com.itany.vo.PermissionTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：sl
 * Date：2019-09-26 下午5:03
 * Description：<描述>
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;


    @Transactional(readOnly = true)
    public PageInfo<Role> findAllPermission(Integer page, Integer rows) {

        PageHelper.startPage(page, rows);
        List<Role> roles = roleMapper.selectAll();
        PageInfo<Role> info = new PageInfo<Role>(roles);

        if (info.getList().size() != 0) {
            for (Role r:info.getList()){
                for (Permission p:r.getPermissionList()){
                    Permission parentPermission=permissionMapper.selectById(p.getParentId());
                    p.setParentPermission(parentPermission);
                }
            }
        }

        return info;
    }

    public  List<PermissionTree> findAllPermissionTree() {

        List<PermissionTree> list = permissionMapper.selectPermissionTree(0);


        return list;
    }
}
